package com.lt.service.impl;

import com.lt.dto.Files;
import com.lt.dto.ProjectApply;
import com.lt.dto.StudentApplyDto;
import com.lt.entity.Project;
import com.lt.entity.Teacher;
import com.lt.entity.Tutor;
import com.lt.mapper.FileMapper;
import com.lt.mapper.ProjectApplyMapper;
import com.lt.mapper.StudentMapper;
import com.lt.mapper.TeacherMapper;
import com.lt.service.ProjectApplyService;
import com.lt.utils.DateTimeUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * @Author :liteng
 * @Time : 2023/10/30 16:07
 */

@Service
@Slf4j
public class ProjectApplyServiceImpl implements ProjectApplyService {

    private final ProjectApplyMapper projectApplyMapper;

    private final StudentMapper studentMapper;

    private final TeacherMapper teacherMapper;

    private final FileMapper fileMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    public ProjectApplyServiceImpl(ProjectApplyMapper projectApplyMapper, StudentMapper studentMapper, TeacherMapper teacherMapper, FileMapper fileMapper) {
        this.projectApplyMapper = projectApplyMapper;
        this.studentMapper = studentMapper;
        this.teacherMapper = teacherMapper;
        this.fileMapper = fileMapper;

    }

    @Transactional
    @Override
    public Long addProject(ProjectApply appliedProject) {
        //项目名称和类型
        String name = appliedProject.getName();
        int category = appliedProject.getCategory();

        //自动生成项目编号
        String number = DateTimeUtils.getDateTime();
        String introduction = appliedProject.getIntroduction();
        LocalDate executionDate = appliedProject.getExecutionDate();

        List<StudentApplyDto> students = appliedProject.getStudents();
        Teacher teacher = appliedProject.getTeacher();
        Tutor tutor = appliedProject.getTutor();
        Files files = appliedProject.getFiles();

        //插入文件数目
        int num = files.getNum();
        log.info("添加了" + num + "个文件");
        List<Files.Data> data = files.getData();

        Project project = new Project(0L, name, category, number, introduction, executionDate, LocalDate.now());
        //插入到数据库，获取到项目id
        if (Boolean.TRUE.equals(redisTemplate.hasKey("myProjects1"))) {
            redisTemplate.delete("myProjects1");
        }

        addOneProject(project);
        Long projectId = project.getId();
        log.info("项目id：" + projectId);

        //添加学生
        StudentApplyDto stuCharge = appliedProject.getPersonInCharge();
//        Long chargeStuId = this.addStu(stuCharge);
        Long chargeStuId = studentMapper.selectStuId(stuCharge.getStuNum());
        log.info("负责人主键" + chargeStuId);
        //添加分工，是否负责人
        projectApplyMapper.insertJoin(chargeStuId, stuCharge, projectId);

        //添加项目成员
        int personNum = appliedProject.getPersonNum();
        log.info("除了负责人，共提交了：" + personNum + "人");
        for (StudentApplyDto stu : students) {
            Long stuId = studentMapper.selectStuId(stu.getStuNum());
            log.info("项目成员id" + stuId);
            projectApplyMapper.insertJoin(stuId, stu, projectId);
        }
        //添加教师
//        Long teacherId = this.addTeacher(teacher);
        Long teacherId = teacherMapper.selectTeacherId(teacher.getTeacherNum());

        //添加 教师  指导
        projectApplyMapper.insertTeacherGuide(teacherId, projectId);

        //添加企业导师
        Long tutorId = this.addTutor(tutor);
        //添加 企业导师 指导
        projectApplyMapper.insertTutorGuide(tutorId, projectId);

        //添加文件
        for (Files.Data data1 : data) {
            fileMapper.insertProjectFile(projectId, data1.getId());
        }

        return projectId;
    }

    @Override
    public void addOneProject(Project project) {
        projectApplyMapper.insertProject(project);

    }


//    @Override
//    public Long addChargeStudent(Student student) {
//        projectApplyMapper.insertStu(student);
//
//        return null;
//    }


    /**
     * 添加项目成员
     *
     * @param students
     * @return
     */
//    @Override
//    public Boolean addStudents(List<Student> students, Long projectId) {
//
//        return null;
//    }

    /**
     * 插入一条学生记录
     *
     * @param student
     * @return 学生主键
     */
//    @Override
//    public Long addStu(Student student) {
//        if (!studentMapper.existStu(student.getStuNum())) {
//            return studentMapper.insertStu(student);
//        } else {
//            log.info("学生已存在");
//            return studentMapper.selectStuId(student.getStuNum());
////            return null;
//        }
//    }


    /**
     * 插入企业导师记录
     *
     * @param tutor
     */
    @Override
    public Long addTutor(Tutor tutor) {
        teacherMapper.insertTutor(tutor);
        return tutor.getId();
    }
}
